<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <link rel="icon" href="images/favicon.ico">
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action=""  id="fm1">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">流水单号</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="payId" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">消费卡号</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="cardId" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <!-- <div class="layui-inline">
                                <label class="layui-form-label">用户ID</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="userId" autocomplete="off" class="layui-input">
                                </div>
                            </div> -->
                            <div class="layui-inline">
                                <label class="layui-form-label">消费类型</label>
                                <div class="layui-input-block">
                                    <select name="payType" lay-filter="aihao">
                                        <option value=""></option>
                                        <option value="0">食堂</option>
                                        <option value="1">超市</option>
                                        <option value="2">跑腿</option>
                                        <!-- <option value="3">音乐</option>
                                        <option value="4">旅行</option> -->
                                    </select>
                                </div>
                                <!-- <label class="layui-form-label">卡状态</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="cardState" autocomplete="off" class="layui-input">
                                </div> -->
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        </div>
    </div>
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table'], function () {
            var table = layui.table,
                form = layui.form,
                $ = layui.jquery,
                laydate = layui.laydate;

            $.ajaxSetup({
                // 发送cookie
                xhrFields: {
                    withCredentials: true
                },
                // headers: {
                //     "token": token
                // }
            })

            table.render({
                elem: '#currentTableId',
                url: 'http://localhost:8080/cardPayLog/cardPayLogList',
                title: '卡号列表',
                toolbar: '#toolbarDemo',
                defaultToolbar: [{
                    title: '导入',
                    layEvent: 'import',
                    icon: 'layui-icon-upload-drag'
                },
                {
                    title: '导出',
                    layEvent: 'export',
                    icon: 'layui-icon-export'
                },
                {
                    title: '模板下载',
                    layEvent: 'templatedownload',
                    icon: 'layui-icon-file'
                }],
                cols: [[
                    { type: "checkbox", width: 50 },
                    { field: 'payId', width: 200, title: '流水单号', sort: true },
                    { field: 'payDate', width: 120, title: '消费时间' },
                    { field: 'payType', width: 150, title: '消费类型' },
                    { field: 'payMoney', width: 100, title: '消费金额' },
                    { field: 'cardId', title: '消费卡号', minWidth: 150 },
                    // { field: 'cardFreezeTime', width: 200, title: '冻结时间' },
                    // {field: 'score', width: 80, title: '评分', sort: true},
                    // {field: 'classify', width: 80, title: '职业'},
                    // {field: 'wealth', width: 135, title: '财富', sort: true},
                    { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,

            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {

                });

                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        // searchParams: result,
                        "payId": data.field.payId,
                        "payType": data.field.payType,
                        "cardId": data.field.cardId,
                        // "cardState": data.field.cardState
                    }
                }, true);

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['80%', '100%'],
                        content: '../card/cardPayLog-add.html',
                        end: function (index) {
                            table.reload('currentTableId');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    layer.msg('delete');
                    //可以在这里实现批量删除    
                    //获选选中行的数组
                    var data = checkStatus.data;
                    console.log(data)
                    var arr = [];    
                    //得到选中行的数据的id  放入一个新的数组
                    for(var i=0;i<data.length;i++){
                        arr.push(data[i].payId);    //   arr   [3,5]
                    }
                    // 数组转字符串
                    var ids = arr.join(",");  //  "3,5"
                    // 把要删除的数据 的 id 拼成的字符串传到后台      
                    $.get("http://localhost:8080/cardPayLog/deleteCardPayLogsByIds",{"ids":ids},function(result){
                        // result 是后台 响应回来的 json 格式的对象   就是后台那个 R 
                        if (result.code == 200) {
                            // 弹出层组件
                            layer.msg("删除成功");
                            //在页面上删除 对应的 行
                            // obj.del(); //删除对应行（tr）的DOM结构
                            // 让表格重新获取下 数据源的数据
                            table.reload('currentTableId');  // 删除完  重新加载下表格
                        } else {
                            layer.msg("删除失败");
                        }  

                    },"json")
                    // var checkStatus = table.checkStatus('currentTableId')
                    //     , data = checkStatus.data;
                    // layer.alert(JSON.stringify(data));
                }else if (obj.event === 'import') {  // 监听删除操作
                    layer.open({
                        title: '学员信息导入',
                        shade: 0.2,
                        type: 2,
                        shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                        area: ['50%', '80%'],
                        fixed: true, //不固定
                        maxmin: true,
                        content: './cardPayLog-import.html',
                        end: function (index) {
                            table.reload('currentTableId');
                        }
                    });
                }else if (obj.event === 'export') {  // 监听删除操作
                    location.href="http://localhost:8080/cardPayLog/export?"+$("#fm1").serialize();
                }else if (obj.event === 'templatedownload') {  // 监听删除操作
                    location.href="http://localhost:8080/cardPayLog/templateDownload";
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {

                    localStorage.setItem("card", JSON.stringify(data));

                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['80%', '100%'],
                        content: '../card/cardPayLog-edit.html',
                        end: function (index) {
                            table.reload('currentTableId');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', { shadeClose: true }, function (index) {
                        $.get("http://localhost:8080/cardPayLog/deleteCardPayLog", { "payId": data.payId }, function (result) {
                            if (result.code == 200) {
                                layer.msg("删除成功");
                                // obj.del(); //删除对应行（tr）的DOM结构
                                table.reload('currentTableId');  // 删除完  重新加载下表格
                            } else {
                                layer.msg("删除失败");
                            }
                        }, "json");
                        layer.close(index);
                    });
                }
            });

        });
    </script>

</body>

</html>